$(document).ready(function () {
	getAllEventsAjax();
	//$('#id_hooligans').chosen();
	var i = 0;
	$("[name=hooligans]").each(function (idx) {
		$(this).attr('id', $(this).attr('id') + i);
		$(this).chosen();
		i++;
	});
	$('#show_add_modal').click(showAddModal);
	$('#submit_add').click(addEventAjax);
	$('#add_offense_button').click(addOffenseAjax);
	$('#show_edit_modal').click(showEditModal);
	$('#show_event_button').click(showEvent);
	$('#show_add_hooligan_modal_button').click(showAddHooliganModa);
	$('#add_hooligan_button').click(addHooliganAjax);
	$('#show_delete_modal_button').click(showDeleteModal);
	$('#delete_button').click(deleteHooliganAjax);
	$('#reload_button').click(getAllEventsAjax);


	$('#filter').keyup(function () {

		var filter = $('#filter').val().toLowerCase();
		console.log(filter);

		$('tr').show();
		if (filter.length == 0) return;

		$('tr:not(:first)').each(function () {
			console.log($(this).find(".name").text());
			if ($(this).find(".offenseType").text().toLowerCase().indexOf(filter) == -1) {

				$(this).hide();
			}
		});

	});
	//  $('#show_profile_button').click(showProfile);
});
function getSelectedId() {
	return $('input[name=tab1]:checked').val()
}
function getAllEventsAjax() {
	$.ajax({
		url: "/api/events/get/all/",
		type: 'POST',
		data: {
			csrfmiddlewaretoken: $.cookie('csrftoken') // just the token value
		},
		success: function (response) {
			$('#events_table_body').html('');
			var events = eval(response);
			for (i = 0; i < events.length; i++) {
				$('#events_table_body').append('<tr>' + '<td><input class="tabradio" type="radio" name="tab1" value="' + events[i].id + '" /></td><td>' + events[i].id + '</td>' + '<td class="offenseType">' + events[i].offenseType + '</td>' + '<td>' + events[i].username + '</td>' + '<td>' + events[i].coordinatesWidth + events[i].coordinatesHeight + '</td>' + '<td>' + events[i].date + '</td>' + '</tr>');
			}
		}
	});
}
function showAddModal() {

	$('#add_event_modal').modal('show');
}
function showEditModal() {
	if (typeof getSelectedId() === 'undefined') {
		alert("You must select row.");
		return;
	}
	$('#add_event_modal').modal('show');

	$.ajax({
		url: "/api/users/get/by/id",
		type: 'POST',
		data: {
			csrfmiddlewaretoken: $.cookie('csrftoken'), // just the token value
			id: getSelectedId()
		},
		success: function (response) {
			var events = eval(response);
			for (i = 0; i < events.length; i++) {
				$('#user_modal_title').html('Edit event: ' + events[i].id + ' <button class="btn btn btn-warning" onclick="resetForm();">Reset</button>');
				$('#id_user').val(users[i].user);
				$('#id_lastname').val(users[i].lastname);
				$('#id_username').val(users[i].username);
				$('#id_address').val(users[i].address);
				$('#id_cardNumber').val(users[i].cardNumber);
				$('#id_phoneNumber').val(users[i].phoneNumber);
				$('#id_privilage').val(users[i].privilage);
			}
		}
	});

}
function showDeleteModal() {
	if (typeof getSelectedId() === 'undefined') {
		alert("You must select row.");
		return;
	}
	$('#delete_modal').modal('show');
}
function showAddHooliganModa() {
	if (typeof getSelectedId() === 'undefined') {
		alert("You must select row.");
		return;
	}
	$('#id_event_id').val(getSelectedId());
	$('#add_hooligan_modal').modal('show');
}

function showEvent() {
	window.location.href = "/events/show/" + getSelectedId();
}
function getSelectedId() {
	return $('input[name=tab1]:checked').val()
}
function addEventAjax() {
	var formData = new FormData(document.getElementById('add_event_form'));
	$.ajax({
		url: '/api/events/create/',
		type: 'POST',
		data: formData,
		cache: false,
		contentType: false,
		processData: false,
		success: function (response) {
			$('#add_event_modal').modal('hide');
			getAllEventsAjax();
		}
	});
}


function addOffenseAjax() {
	alert('');
	var formData = new FormData(document.getElementById('add_offense_form'));
	$.ajax({
		url: '/api/offense/create/',
		type: 'POST',
		data: formData,
		cache: false,
		contentType: false,
		processData: false,
	});
}

function editEventAjax() {
	$('#event_id').val(getSelectedId());
	var formData = new FormData(document.getElementById('add_event_form'));
	$.ajax({
		url: '/api/events/create/',
		type: 'POST',
		data: formData,
		cache: false,
		contentType: false,
		processData: false
	});
}
function addHooliganAjax() {
	var formData = new FormData(document.getElementById('add_hooligan_form'));
	$.ajax({
		url: '/api/events/add/hooligan/',
		type: 'POST',
		data: formData,
		cache: false,
		contentType: false,
		processData: false
	});
}
function deleteHooliganAjax() {
	$.ajax({
		url: '/api/events/delete/',
		type: 'POST',
		data: {
			csrfmiddlewaretoken: $.cookie('csrftoken'), // just the token value
			event_id: getSelectedId()
		},
		success: function () {
			$('#delete_modal').modal('hide');
			getAllEventsAjax();
		}
	});
}